﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Prime.Models;

namespace Prime.Controllers
{
    public class StudentLessonProgressController : ApiController
    {
        private LearningDbEntities db = new LearningDbEntities();

        // GET api/StudentLessonProgress
        public IQueryable<StudentLessonProgress> GetStudentLessonProgresses()
        {
            return db.StudentLessonProgresses;
        }

        // GET api/StudentLessonProgress/5
        [ResponseType(typeof(StudentLessonProgress))]
        public IHttpActionResult GetStudentLessonProgress(int id)
        {
            var studentlessonprogress = from d in db.StudentLessonProgresses
                                            where d.UserName == User.Identity.Name && d.LessonId == id
                                            select d;
            if (!studentlessonprogress.Any())
            {
                return NotFound();
            }

            return Ok(studentlessonprogress.First());
        }

        // PUT api/StudentLessonProgress/5
        public IHttpActionResult PutStudentLessonProgress(int id, StudentLessonProgress studentlessonprogress)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != studentlessonprogress.LessonId || User.Identity.Name != studentlessonprogress.UserName)
            {
                return BadRequest();
            }

            db.Entry(studentlessonprogress).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentLessonProgressExists(User.Identity.Name))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/StudentLessonProgress
        [ResponseType(typeof(StudentLessonProgress))]
        public IHttpActionResult PostStudentLessonProgress(StudentLessonProgress studentlessonprogress)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.StudentLessonProgresses.Add(studentlessonprogress);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (StudentLessonProgressExists(studentlessonprogress.UserName))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = studentlessonprogress.UserName }, studentlessonprogress);
        }

        // DELETE api/StudentLessonProgress/5
        [ResponseType(typeof(StudentLessonProgress))]
        public IHttpActionResult DeleteStudentLessonProgress(string id)
        {
            StudentLessonProgress studentlessonprogress = db.StudentLessonProgresses.Find(id);
            if (studentlessonprogress == null)
            {
                return NotFound();
            }

            db.StudentLessonProgresses.Remove(studentlessonprogress);
            db.SaveChanges();

            return Ok(studentlessonprogress);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool StudentLessonProgressExists(string id)
        {
            return db.StudentLessonProgresses.Count(e => e.UserName == id) > 0;
        }
    }
}